package com.baomidou.springboot.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.springboot.entity.News;
import com.baomidou.springboot.entity.Notice;
import com.baomidou.springboot.mapper.NoticeMapper;
import com.baomidou.springboot.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 重要通知 服务实现类
 * </p>
 *
 * @author QQQ
 * @since 2018-06-19
 */
@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public void save(Notice notice) {
        notice.setCreateDate(new Date());
        noticeMapper.insert(notice);
    }

    @Override
    public void delete(String id) {
        noticeMapper.deleteById(id);
    }

    @Override
    public Notice edit(String id) {
        return noticeMapper.selectById(id);
    }

    @Override
    public void update(Notice notice) {
        notice.setModifyDate(new Date());
        noticeMapper.updateById(notice);
    }

    @Override
    public Notice view(String id) {
        return noticeMapper.selectById(id);
    }

    @Override
    public Page<Notice> list(Page<Notice> page, String search) {
        if (0 == page.getSize()) {
            page.setSize(10);
        }
        if (0 == page.getCurrent()) {
            page.setCurrent(1);
        }
        List<Notice> noticeList = noticeMapper.findByDateDesc(page, search);
        page.setRecords(noticeList);
        return page;
    }

    @Override
    public void deleteBatch(List<String> ids) {
        if (null == ids) {
            return;
        }
        noticeMapper.deleteBatchIds(ids);
    }

    @Override
    public List<Notice> findByLimit(Integer limit) {
        return noticeMapper.findByLimit(limit);
    }

    @Override
    public Notice findById(String id) {
        return noticeMapper.selectById(id);
    }
}
